package com.plugin.planilha;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class AnaliseDefasadaHashtableSheet implements ISheet {

	private WritableWorkbook workbook;
	private HashMap<String, HashSet<String>> mapHashtable;
	private HashMap<String, HashSet<String>> mapHashmap;
	private static WritableSheet defasadaHashtableWS;

	public AnaliseDefasadaHashtableSheet(WritableWorkbook w,
			HashMap<String, HashSet<String>> map_defasada,
			HashMap<String, HashSet<String>> map_usada) {
		workbook = w;
		this.mapHashtable = map_defasada;
		this.mapHashmap = map_usada;
		defasadaHashtableWS = workbook.createSheet(
				"Defasada Hashtable e Hashmap", 1);
	}

	@Override
	public void addDados() throws RowsExceededException, WriteException {
		Label label;
		jxl.write.Number n;
		int indice = 1;
		double num_variaveis_defasadas = 0, num_variaveis_usadas = 0;

		for (String classe : mapHashmap.keySet()) {
			if (mapHashmap.get(classe).size() > 0) {
				Iterator<String> iterator = mapHashmap.get(classe).iterator();
				while (iterator.hasNext()) {
					String var = iterator.next();

					label = new Label(0, indice, var);
					defasadaHashtableWS.addCell(label);
					indice++;
				}
			}
		}
		indice = 1;
		for (String clsse : mapHashtable.keySet()) {
			if (mapHashtable.get(clsse).size() > 0) {
				Iterator<String> iterator = mapHashtable.get(clsse).iterator();
				while (iterator.hasNext()) {
					String var = iterator.next();

					label = new Label(1, indice, var);
					defasadaHashtableWS.addCell(label);
					indice++;
				}
			}
		}

		for (String classe : mapHashtable.keySet())
			num_variaveis_defasadas += mapHashtable.get(classe).size();
		for (String classe : mapHashmap.keySet())
			num_variaveis_usadas += mapHashmap.get(classe).size();

		double round_comparacao = 0;
		if (num_variaveis_defasadas != 0) {
			round_comparacao = (double) Math
					.round(((num_variaveis_defasadas * 100) / (num_variaveis_usadas + num_variaveis_defasadas)) * 1000) / 1000;
		}

		n = new jxl.write.Number(3, 1, (int) num_variaveis_defasadas);
		defasadaHashtableWS.addCell(n);

		n = new jxl.write.Number(4, 1, (int) num_variaveis_usadas);
		defasadaHashtableWS.addCell(n);

		n = new jxl.write.Number(5, 1, round_comparacao);
		defasadaHashtableWS.addCell(n);

	}

	@Override
	public void criaCabecalho(WritableCellFormat cf)
			throws RowsExceededException, WriteException {

		Label label = new Label(0, 0, "Variaveis HashMap", cf);
		defasadaHashtableWS.addCell(label);

		label = new Label(1, 0, "Variaveis Hashtable", cf);
		defasadaHashtableWS.addCell(label);

		label = new Label(3, 0, "Numero de instancias Hashtable", cf);
		defasadaHashtableWS.addCell(label);

		label = new Label(4, 0, "Numero instancias de HashMap", cf);
		defasadaHashtableWS.addCell(label);

		label = new Label(5, 0, "Taxa de comparacao", cf);
		defasadaHashtableWS.addCell(label);
	}

}
